(function() {


var _traceq = _traceq || [];
var traceUrl = "http://trail.lri.fr/trace";
// "http://localhost:5000/trace";
var _sending;
var sessionId;
var starting = true;
var debug = false;

trace = {version: "0.1"};

trace.getUrl = function() {
    return traceUrl;
};

trace.setUrl = function(url) {
    traceUrl = url;
};

trace.getSessionId = function() {
    return sessionId;
};

trace.debug = function(d) {
    debug =d;
}

var uuid = function() {
  var uuid = "", i, random;
  for (i = 0; i < 32; i++) {
    random = Math.random() * 16 | 0;

    if (i == 8 || i == 12 || i == 16 || i == 20) {
      uuid += "-"
    }
    uuid += (i == 12 ? 4 : (i == 16 ? (random & 3 | 8) : random)).toString(16);
  }
  return uuid;
}

function ajaxRequest(){
    var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] //activeX versions to check for in IE
    if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
	for (var i=0; i<activexmodes.length; i++){
	    try{
		return new ActiveXObject(activexmodes[i]);
	    }
	    catch(e){
		//suppress error
	    }
	}
    }
    else if (window.XMLHttpRequest) // if Mozilla, Safari etc
	return new XMLHttpRequest();
    else
	return false;
}


var sendLogs_ = function(list) {
    var httpRequest = ajaxRequest();
    httpRequest.onreadystatechange = function() {
	//if (debug)
	//    console.log("readyState =%d", httpRequest.readyState);
	if (httpRequest.readyState == 4) {
	    //if (debug)
	    //  console.log("status =%d", httpRequest.status);
	    sendMoreOrAgain(httpRequest.status < 300);
	}
    };
    var json = JSON.stringify(list);
    httpRequest.open("POST", traceUrl);
    httpRequest.setRequestHeader("Content-Type", "application/json");
    httpRequest.setRequestHeader("Accept", "text/plain");
//    httpRequest.setRequestHeader("Content-Length", json.length);
    httpRequest.send(json);
}

var sendLogs = function() {
    if (_traceq.length == 0) return;
    _sending = _traceq;
    //if (debug)
	//console.log("Sending %d messages", _sending.length);
    _traceq = [];
    sendLogs_(_sending);
}

var sendMoreOrAgain = function(ok) {
    if (ok) {
	_sending = null;
	sendLogs();
    }
    else {
	if (_traceq.length != 0) {
	    _sending = _sending.concat(_traceq);
	    _traceq = [];
	}
	//if (debug)
	  //  console.log("Re-sending %d messages", _sending.length);
	sendLogs_(_sending); // try again
    }
}


//_traceq.push(['_setAccount', 'UA-29614248-1']);

function traceEvent(cat, action, label, value) {
    if (starting) {
	starting = false;
	_sending = [];
	traceEvent("_trace", "document", "url", document.location.href);
	_sending = null;
    }

    //if (debug)
	//console.log("Track["+cat+","+action+","+label+"]");
    //var ts = Date.now();
    var date = new Date();
    var ts = date.getTime();
    _traceq.push({"session": sessionId,
		  "ts": ts,
		  "cat": cat,
		  "action": action,
		  "label": label,
		  "value": value});
    if (_sending == null)
	sendLogs();
}

    trace.event = traceEvent;
    sessionId = uuid();

//    console.log("Trace initialized with sessionId=%s", sessionId);

})();
